package com.xrh.core.generater.bean;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.xrh.core.generater.interfaces.MarkReplaceInf;
import com.xrh.core.generater.utils.MyStringUtils;
import com.xrh.core.generater.utils.RegexUtils;

import java.util.Set;

import lombok.Data;

/**
* @function 表信息类
* @author 肖荣辉
* @date 2018年9月16日
*/
@Data
public class TableInfo implements MarkReplaceInf{
	
	//主键集合
	private Collection<String> primaryKeys;
	
	//表名
	private String tableName;
	
	//表注释
	private String comment;
	
	//字段信息map
	LinkedHashMap<String , ColumnBean> columnInfoMap;
	
	//类名
	private String className;
	
	//首字母小写类名
	private String littleClassName;
	
	//属性信息map
	LinkedHashMap<String , ColumnBean> propertyInfoMap;
	
	//第一个主键ID列名称
	private String firstPrimaryKeyName;
	
	//第一个主键属性名
	private String firstPrimaryKeyPropertyName;
	
	//第一个主键属性名,首字母大写
	private String bigFirstPrimaryKeyPropertyName;
	
	//主键列bean
	private ColumnBean primaryColumnBean;
	
	//删除列bean
	private ColumnBean isDeletedColumnBean;
	
	//已经删除的标志值
	private String isDeletedValue;
	
	//创建日期bean
	private ColumnBean createDateBean;

	//创建人列bean
	private ColumnBean createManBean;
	
	//更新人列bean
	private ColumnBean updateManBean;
	
	public TableInfo() {
		super();
	}

	public TableInfo(String tableName, String comment) {
		super();
		this.tableName = tableName;
		this.comment = comment;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Map<String, ColumnBean> getColumnInfoMap() {
		return columnInfoMap;
	}

	public void setColumnInfoMap(LinkedHashMap<String, ColumnBean> columnInfoMap) {
		this.columnInfoMap = columnInfoMap;
	}

	public Collection<String> getPrimaryKeys() {
		return primaryKeys;
	}

	public void setPrimaryKeys(Collection<String> primaryKeys) {
		this.primaryKeys = primaryKeys;
	}
	
	//获取属性对照表
	public Map<String, ColumnBean> getPropertyInfoMap() {
		
		if(propertyInfoMap != null)
			return propertyInfoMap;
		
		propertyInfoMap = new LinkedHashMap<>();
		
		Set<Entry<String, ColumnBean>> set = columnInfoMap.entrySet();
		
		for(Entry<String, ColumnBean> item : set){
			propertyInfoMap.put(item.getValue().getPropertyName(), item.getValue());
		}
		
		return propertyInfoMap;
	}

	public void setPropertyInfoMap(LinkedHashMap<String, ColumnBean> propertyInfoMap) {
		this.propertyInfoMap = propertyInfoMap;
	}

	
	//获取第一个主键列名
	public String getFirstPrimaryKeyName() {
		
		if(firstPrimaryKeyName != null) 
			return firstPrimaryKeyName;
		
		if(primaryKeys.size() >0)
			firstPrimaryKeyName = primaryKeys.iterator().next();
		
		return firstPrimaryKeyName == null ? "" : firstPrimaryKeyName;
	}

	//获取第一个主键属性名
	public String getFirstPrimaryKeyPropertyName() {
		
		if(firstPrimaryKeyPropertyName != null) 
			return firstPrimaryKeyPropertyName;
		
		firstPrimaryKeyPropertyName = RegexUtils.generatePropertyName(getFirstPrimaryKeyName());
		
		return firstPrimaryKeyPropertyName == null ? "" : firstPrimaryKeyPropertyName;
		
	}
	
	//获取第一个主键属性名,首字母大写
	public String getBigFirstPrimaryKeyPropertyName() {
		
		if(bigFirstPrimaryKeyPropertyName != null) 
			return bigFirstPrimaryKeyPropertyName;
		
		bigFirstPrimaryKeyPropertyName = MyStringUtils.uppercaseIndexLetter(getFirstPrimaryKeyPropertyName());
		
		return bigFirstPrimaryKeyPropertyName;
	}
	
	//获取主键列bean
	public ColumnBean getPrimaryColumnBean() {
	
		if(primaryColumnBean != null) 
			return primaryColumnBean;
		
		primaryColumnBean = getPropertyInfoMap().get(getFirstPrimaryKeyPropertyName());
		 
		return primaryColumnBean;
	}

	//获取删除列bean
	public ColumnBean getIsDeletedColumnBean(String deleteColumnName) {
		
		if(isDeletedColumnBean != null) 
			return isDeletedColumnBean;
		
		 isDeletedColumnBean =getColumnInfoMap().get(deleteColumnName);
		 
		 if(isDeletedColumnBean == null)
			 isDeletedColumnBean = getColumnInfoMap().get("isDeleted".equals(deleteColumnName) && getColumnInfoMap().containsKey(deleteColumnName) ? "isDeleted" : "is_deleted");
		 
		 if(isDeletedColumnBean == null)
			 isDeletedColumnBean = getColumnInfoMap().get("is_deleted".equals(deleteColumnName) && getColumnInfoMap().containsKey(deleteColumnName) ? "is_deleted" : "isDeleted");
		 
		return isDeletedColumnBean;
	}
	
	//获取创建日期列的bean	
	public ColumnBean getCreateDateBean(String createDateColumnName) {
		
		if(createDateBean != null) 
			return createDateBean;
		
		createDateBean =getColumnInfoMap().get(createDateColumnName);
		 
		 if(createDateBean == null)
			 createDateBean = getColumnInfoMap().get("createDate".equals(createDateColumnName) && getColumnInfoMap().containsKey(createDateColumnName) ? "createDate" : "gmt_create");
		 
		 if(createDateBean == null)
			 createDateBean = getColumnInfoMap().get("gmt_create".equals(createDateColumnName) && getColumnInfoMap().containsKey(createDateColumnName) ? "gmt_create" : "createDate");
		
		return createDateBean;
	}
	
	//获取创建日期列的bean
	public ColumnBean getCreateManBean(String createManColumnName) {
		
		if(createManBean != null) 
			return createManBean;
		
		createManBean =getColumnInfoMap().get(createManColumnName);
		 
		 if(createManBean == null)
			 createManBean = getColumnInfoMap().get("createMan".equals(createManColumnName) && getColumnInfoMap().containsKey(createManColumnName) ? "createMan" : "create_operator_id");
		 
		 if(createManBean == null)
			 createManBean = getColumnInfoMap().get("create_operator_id".equals(createManColumnName) && getColumnInfoMap().containsKey(createManColumnName) ? "create_operator_id" : "createMan");
		 
		return createManBean;
	}
	
	//获取创建日期列的bean
	public ColumnBean getUpdateManBean(String updateManColumnName) {
		
		if(updateManBean != null) 
			return updateManBean;
		
		updateManBean =getColumnInfoMap().get(updateManColumnName);
		 
		 if(updateManBean == null)
			 updateManBean = getColumnInfoMap().get("updateMan".equals(updateManColumnName) && getColumnInfoMap().containsKey(updateManColumnName) ? "updateMan" : "modify_operator_id");
		 
		 if(updateManBean == null)
			 updateManBean = getColumnInfoMap().get("modify_operator_id".equals(updateManColumnName) && getColumnInfoMap().containsKey(updateManColumnName) ? "modify_operator_id" : "updateMan");
		 
		return updateManBean;
	}

	//根据删除列的类型,获取删除标志的值,因为类型不同
	public String getIsDeletedValue(String deleteColumnName) {
		
		if(isDeletedValue != null) 
			return isDeletedValue;
		
		isDeletedValue = getIsDeletedColumnBean(deleteColumnName).getJavaTypeName().equals("Boolean") ? "true" : "1";
		
		return isDeletedValue;
	}

	@Override
	public String getBigClassName() {
		return className;
	}

	@Override
	public String getSmallClassName() {
		return littleClassName;
	}

	@Override
	public String getTableComment() {
		return comment;
	}
	
	
}
